use std::collections::HashMap;

#[derive(Clone)]
pub struct Router {
    pub method_entries: HashMap<&'static str, ServiceMethods>, // key:方法名称 value:服务方法列表
}

impl Router {
    pub fn new() -> Router {
        let mut r = Self {
            method_entries: HashMap::new(),
        };
        r.init();
        r
    }
    fn init(&mut self) {
        let mut service_methods: HashMap<String, ServiceMethods> = HashMap::new();
        service_methods.insert(
            "asset_service".to_string(),
            ServiceMethods {
                name: "asset_service",
                endpoint: "[::1]:50051",
                methods: vec!["asset.update", "asset.query"],
            },
        );

        for (_, methods) in service_methods.iter() {
            for method in methods.methods.iter() {
                self.method_entries.insert(method, methods.clone());
            }
        }
    }

    pub fn route(&self, method: &str) -> Option<&'static str> {
        let s = self.method_entries.get(method);
        match s {
            Some(s) => Some(s.endpoint),
            None => None,
        }
    }
}

#[derive(Clone)]
struct ServiceMethods {
    pub name: &'static str,
    pub endpoint: &'static str,
    pub methods: Vec<&'static str>,
}
